package dao;

import domain.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

//操作数据库中user表的类
public class UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    //查询表中记录,返回一个把记录封装的对象
    public User login(User loginUser) {
        try {
            //sql语句
            String sql = "select*from user where username = ? and password = ?";
            //执行查询语句
            User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>
                    (User.class), loginUser.getUsername(), loginUser.getPassword());
            return user;
        } catch (DataAccessException e) {
            return null;
        }
    }
}

